//
//  AppDelegate.m
//  01-UIView、UIWindow
//
//  Created by QC.L on 16/7/14.
//  Copyright © 2016年 QC.L. All rights reserved.
//

#import "AppDelegate.h"
#import "ViewController.h"
#import "UIView+Frame.h"
@interface AppDelegate ()<UITextFieldDelegate>

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    // 程序的组成结构
    // 1.main.m 主要实现了程序的正常运行
    // 2.AppDelegate 程序的执行者, 签订了UIApplicationDelegate
    // 注意: AppDelegate 就是一个普通的类, 只不过签订协议, 才有了这么多的方法
    // 3.ViewController 视图控制器 主要负责视图管理 看得见摸得到的
    // 4.Main.sb(视图管理) LaunchScreen.sb(负责启动页) 可视化管理 (Xcode 7 以前叫LaunchScreen.xib)
    // 5.Assets.xcassets 主要用来管理图片素材(Xocde 7以前叫Images.xcassets)
    // 6.Info.plist (工程配置文件)
    
    // 模拟器
    // command + 1~5 改变模拟器 大小
    // command + ←/→ 横竖屏切换
    
    // iOS概述
    
    // 1.UIWindow(程序中只有一个主window)
    //  1> 取消程序的可视化入口, Main.sb
    //  2> 创建主window对象
    // 创建主窗口使其大小和屏幕一样
    // UIScreen 系统屏幕类
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    // 给你创建的窗口填颜色 UIColor 系统的颜色类
    _window.backgroundColor = [UIColor whiteColor];
    // 让window 成为主窗口 并 显示
    [_window makeKeyAndVisible];
    // Xcode 7 以后, 必须设置rootViewConroller, 否则, 在该方法结束时会崩溃
    _window.rootViewController = [[ViewController alloc] init];
    
    // 3> UIWindow 继承于UIView
    
    // 2.UIView(与NSObject相似, 大部分的UI控件继承于UIView)
    // 学习新类
    //   1> 需要新类包含哪些初始方法
    //   2> 包含哪些行为(方法) 和特性(属性)
    // 创建UIView
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 250, 250)];
    // 设置背景色
    view.backgroundColor = [UIColor greenColor];
    // 添加到window上
    [_window addSubview:view];
    // 遇到UIView的子类, 都可以用以上的方式创建
    // UIView在屏幕中显示为矩形
    
    // iOS系统坐标系
    // 1.与数学的坐标系不同, y轴的正方向向下, 远点是屏幕的坐上角
    // 2.原点是屏幕的左上角
    
    // frame bounds center(UIView 的属性)
    // 1.frame (x, y, width, height) 视图在其父视图上的位置
    // 2.bounds (x, y, width, height) 视图本身的坐标系
    // 3.center 中心点
//    view.frame = CGRectMake(10, 10, 80, 80);
    // bounds 对视图的影响测试
    // CGRect 和 CGPoint
    
    NSLog(@"改变bounds前: %@", NSStringFromCGRect(view.frame));
    NSLog(@"改变前的center: %@", NSStringFromCGPoint(view.center));
    // bounds x, y修改的本身的坐标系, 影响他的子视图
    // bounds width, height 修改的本身的大小会以中心点收缩
    view.bounds = CGRectMake(10, 10, 100, 100);
    NSLog(@"改变bounds后: %@", NSStringFromCGRect(view.frame));
    NSLog(@"改变后的center: %@", NSStringFromCGPoint(view.center));
    
//    UIView *subView = [[UIView alloc] initWithFrame:CGRectMake(30, 30, 80, 80)];
//    subView.backgroundColor = [UIColor yellowColor];
//    [view addSubview:subView];
    
    // 改变绿色view的大小, width 变成 100
    view.frame = CGRectMake(view.frame.origin.x, view.frame.origin.y, 100, view.frame.size.height);
    // view.frame get方法
    // frame.size 结构体
    // OC和C的内容结合, 是一个常量
//    view.frame.size.width = 100;
    
    CGRect rect = view.frame;
    rect.size.width = 100;
    view.frame = rect;
    view.h_height = 500;
    
    // UIView的基本属性
    UIView *testView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    [_window addSubview:testView];
    // 背景颜色
    testView.backgroundColor = [UIColor yellowColor];
    // 2.alpha 透明度 0~1 默认值 1.0
    testView.alpha = 0.2;
    // 3.tag值 标记(编号) 默认为0
    testView.tag = 10;
    // 根据tag值获取到的view __kingof
    UIView *view1 = [_window viewWithTag:10];
    // 4.subViews
    UIView *arrayView = [_window.subviews objectAtIndex:0];
    NSLog(@"%@", arrayView);
    // 5.superView
    
//    testView.superview;
    // 6.hidden 隐藏/显示
    testView.hidden = NO;
    
    // 视图层级
    UIView *firstView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 120, 120)];
    firstView.backgroundColor = [UIColor blackColor];
    UIView *secondView = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 300, 50)];
    secondView.backgroundColor = [UIColor grayColor];
    UIView *thirdView = [[UIView alloc] initWithFrame:CGRectMake(300, 200, 70, 220)
                         ];
    thirdView.backgroundColor = [UIColor redColor];
    // addSubview(添加视图)
    [_window addSubview:firstView];
//    NSLog(@"%@", _window.subviews);
    // insertSubview:atIndex:(插入视图, 到指定下标)
    [_window insertSubview:secondView atIndex:2];
    // insertSubview:aboveSubview:(插入视图, 到指定视图之上)
    [_window insertSubview:thirdView aboveSubview:secondView];
    // insertSubview:belowSubview:(插入视图, 到指定视图之上)
    
    // UILabel(负责显示文本)
    // 如果类没有自己的初始化方法, 则使用父类的
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 100, 300, 100)];
    label.backgroundColor = [UIColor redColor];
    [_window addSubview:label];
    
    // label的属性
    // 1.text
    label.text = @"我是王鍠, 我很帅asdasdasdjadhfdskjhafaskljdfkladsljkfhaadfkhasdklfasakdfkadsfjlasdjfla;sdlfs;dlajf;ladsjfl;jsdakflk;jasdjlkfasdkljf;kjfal;djfl;ajdsadsfljlaksdasdhaskjdhaslkdhas";
    // 2.textColor 字体颜色
    label.textColor = [UIColor blueColor];
    // 3.font 字体 默认值17
    label.font = [UIFont systemFontOfSize:30];
    label.font = [UIFont systemFontOfSize:15 weight:40];
    // 4.textAlignment 对其方式
    // left right center
    // 默认对齐方式left
    label.textAlignment = NSTextAlignmentCenter;
    // 5.numberOfLines 默认值是1
    // 不确定有多少行的时候, 设置0
    label.numberOfLines = 0;
    // 6.lineBreakMode 换行模式, 省略号位置
    label.lineBreakMode = NSLineBreakByTruncatingMiddle;
    // 7.阴影
//    label.shadowColor = [UIColor blackColor];
//    label.shadowOffset = CGSizeMake(1, 2);
    
    
    
    
    // UIButton(用来点击的)
    // UIButtonTypeSystem 系统的按钮类型
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.backgroundColor = [UIColor cyanColor];
    [_window addSubview:button];
    button.h_x = 0;
    button.h_y = 0;
    button.h_width = 100;
    button.h_height = 100;
    
    // 给button添加事件
    [button addTarget:self action:@selector(clicked) forControlEvents:UIControlEventTouchUpInside];
    
    [button setTitle:@"点我啊" forState:UIControlStateNormal];
    
    [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    
    button.titleLabel.font = [UIFont systemFontOfSize:30];
    
    // UITextField
    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(100, 400, 200, 300)];
    textField.backgroundColor = [UIColor grayColor];
    // 1.text 给的一个初值
//    textField.text = @"请输入文字";
    // 2.textColor 文字颜色
    textField.textColor = [UIColor redColor];
    // 3.borderStyle
    // UITextBorderStyleLine 有边缘线
    textField.borderStyle = UITextBorderStyleLine;
    // UITextBorderStyleBezel
    textField.borderStyle = UITextBorderStyleBezel;
    // UITextBorderStyleRoundedRect 带圆角的
    textField.borderStyle = UITextBorderStyleRoundedRect;
    // 4.placeHolder 占位符
    textField.placeholder = @"请输入您的爱好";
    [_window addSubview:textField];
    // 5.clearsOnBeginEditing
    textField.clearsOnBeginEditing = YES;
    textField.font = [UIFont systemFontOfSize:30];
    textField.delegate = self;
    
    // command + k 开启/收起 键盘
    
    
    return YES;
}
// 开始编辑前, 判断能否进行编辑
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    // 可以编辑
//    return NO;
    // 不可以编辑
    return YES;
}
// 已经开始编辑时
- (void)textFieldDidBeginEditing:(UITextField *)textField {
    
}
//
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
    
    
    return YES;
}
// 已经结束编辑
- (void)textFieldDidEndEditing:(UITextField *)textField {
    
}
// 处理点击return需要进行的操作
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    // 回收键盘
    [textField endEditing:YES];
    return YES;
}


- (void)clicked {
    // 结束编辑
    [_window endEditing:YES];
    NSLog(@"ccccc");
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
